package org.samcolon.eeexplore.utils;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.StrUtil;

import java.time.LocalDateTime;


/**
 * @Description: 日期工具方法
 * @author: Sam.zhang
 * @date: 2021/11/26 - 10:54
 */
public class SamDateUtil {

    public static void main(String[] args) {
        LocalDateTime exp = LocalDateTime.of(2023, 10, 10, 1, 2);
        LocalDateTime now = LocalDateTime.now();

        Console.log(exp.isBefore(now));

        SamDateUtil du = new SamDateUtil();
        Console.log(du.getTimeStrBySecond(87744L));
        Console.log(du.getTimeStrBySecond(44L));
        Console.log(du.getTimeStrBySecond(7744L));
        Console.log(du.getTimeStrBySecond(744L));


        Console.log("beginOfYear : {}", DateUtil.beginOfYear(DateUtil.date().offset(DateField.YEAR, 1)));
        Console.log("dayOfYear : {}", DateUtil.dayOfYear(DateUtil.date().offset(DateField.YEAR, 1)));


        du.test();
    }

    private void test(){
        DateTime now = DateTime.now();
        DateTime nextYearDate = DateTime.now().setField(DateField.YEAR, now.year() + 1);
        Console.log("now : {}  next: {}", now, nextYearDate);
        DateTime expDate = DateUtil.beginOfYear(nextYearDate);
        Console.log("exp date : {}", expDate);
        Console.log(expDate.isAfter(now));
        Console.log(now.getTime());
    }

    private String getTimeStrBySecond(Long totalSecond) {
        if (totalSecond == null || totalSecond <= 0) {
            return "00:00:00";
        }
        int maxSecond = 60;
        int maxMinute = 60;
        if (totalSecond < maxSecond) {
            return "00:00:" + fillTimeStr(totalSecond.intValue());
        }

        int second = (int) (totalSecond % maxSecond);
        int minute = (int) (totalSecond / maxSecond);
        if (minute < maxMinute) {
            return StrUtil.join(":", "00", fillTimeStr(minute), fillTimeStr(second));
        }
        int hour = minute / maxMinute;
        minute = minute % maxMinute;
        return StrUtil.join(":", fillTimeStr(hour), fillTimeStr(minute), fillTimeStr(second));
    }

    private String fillTimeStr(int time) {
        return time < 10 ? "0" + time : String.valueOf(time);
    }
}
